#include "login_register_logic.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>

LoginRegisterLogic::LoginRegisterLogic(LoginRegisterUi *ui, QObject *parent)
    : QObject(parent), ui(ui)
{
    // 连接按钮点击事件到槽函数
    connect(ui->loginButton, &QPushButton::clicked, this, &LoginRegisterLogic::onLoginClicked);
    connect(ui->registerButton, &QPushButton::clicked, this, &LoginRegisterLogic::onRegisterClicked);
    connect(ui->forgotPasswordButton,
            &QPushButton::clicked,
            this,
            &LoginRegisterLogic::onForgotPasswordClicked);
}
void LoginRegisterLogic::Loginfunction(PptpMessage responseMessage)
{
    QJsonObject responseParameters = responseMessage.getParameters();
    qDebug() << responseParameters;

    int userid = responseParameters.value("userId").toInt();
    qDebug() << userid;
    QString message = responseParameters.value("message").toString();

    if (userid != -1)
    {
        UserSession::getInstance().setUserId(userid); // 保存用户id
        QMessageBox::information(nullptr,
                                 "登录成功",
                                 "欢迎 " + responseParameters.value("userInfo").toString() + "\n您的用户ID是: " + QString::number(userid));
        ui->close();
        JkjUi *mainUi = new JkjUi();
        JkjUiLogic *mainLogic = new JkjUiLogic(mainUi);
        mainUi->show();
    }
    else
    {
        QMessageBox::warning(nullptr, "登录失败", message);
    }
}
void LoginRegisterLogic::onLoginClicked()
{
    QString username = ui->usernameEdit->text();
    QString password = ui->passwordEdit->text();
    QJsonObject parameters;
    parameters["loginInfo"] = username;
    parameters["password"] = password;
    requestMessage.clear();
    requestMessage.setParameters(parameters);
    requestMessage.setPath("user/login");
    // QByteArray file;
    // requestMessage.addFile(file);
    pptpclient.pptpConnect(IP, PORT, requestMessage, this, &LoginRegisterLogic::Loginfunction);
}

void LoginRegisterLogic::Registerfunction(PptpMessage responseMessage)
{
    QJsonObject responseParameters = responseMessage.getParameters();
    int userid = responseParameters.value("userId").toInt();
    qDebug() << responseParameters;
    qDebug() << userid;
    QString message = responseParameters.value("message").toString();
    // 根据 userid 判断注册是否成功
    if (userid != -1)
    {
        QMessageBox::information(nullptr,
                                 "注册成功",
                                 "欢迎注册，您的用户ID是: " + QString::number(userid));
    }
    else
    {
        QMessageBox::warning(nullptr, "注册失败", message);
    }
}
void LoginRegisterLogic::onRegisterClicked()
{
    QString registerInfo = ui->usernameEdit->text();
    QString password = ui->passwordEdit->text();
    QJsonObject parameters;
    parameters["registerInfo"] = registerInfo;
    parameters["password"] = password;
    // PptpMessage requestMessage;
    requestMessage.clear();
    requestMessage.setParameters(parameters);
    requestMessage.setPath("user/register");
    pptpclient.pptpConnect(IP, PORT, requestMessage, this, &LoginRegisterLogic::Registerfunction);
}

void LoginRegisterLogic::onForgotPasswordClicked()
{
    // QMessageBox::information(nullptr, "忘记密码", "忘记密码功能暂未实现");
}
